<template>
  <van-icon
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    :class="{
      liked: value === 1
    }"
    :loading="Loading"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from '../../api/user'
export default {
  data() {
    return {
      Loading: false
    }
  },

  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onLike() {
      this.Loading = true
      try {
        let status = -1
        if (this.value === 1) {
          await deleteLike(this.articleId)
        } else {
          await addLike(this.articleId)
          status = 1
        }
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast('操作失败')
      }
      this.Loading = false
    }
  }
}
</script>

<style scoped lang="less">
.liked {
  color: #e5645f;
}
</style>
